// pages/complaint-form/complaint-form.js
import api from "../../../utils/request"
Page({
  data: {
    formData: {
      type: "0", // 默认投诉
      houseId: "",
      houseAddress: "",
      description: "",
    },
    submitting: false,
    canSubmit: false,

    // 选择器状态
    showHouse: false,

    // 房屋选项
    houseOptions: [
      { text: "城市花园1栋1单元1层101", value: "1" },
      { text: "城市花园1栋2单元2层201", value: "2" },
      { text: "城市花园2栋1单元3层301", value: "3" },
    ],
  },

  onLoad() {
    this.loadUserHouses()
    this.checkCanSubmit()
  },

  // 加载用户房屋
  loadUserHouses() {
    wx.showLoading({ title: "加载中..." });
    api.get("/property/house/myHouses").then((res) => {
      if (res.code === 200) {
        const houseOptions = res.data.map((item) => ({
          text: `${item.communityName} ${item.buildingName} ${item.unitName} ${item.floor} ${item.doorNumber}`,
          value: item.roomId.toString(),
        }));
        this.setData({ houseOptions });
      } else {
        wx.showToast({ title: "加载房屋失败", icon: "none" });
      }
      wx.hideLoading();
    }).catch((err) => {
      console.error(err);
      wx.hideLoading();
      wx.showToast({ title: "加载房屋失败", icon: "none" });
    });
  },

  // 类型变化
  onTypeChange(e) {
    this.setData({
      "formData.type": e.detail,
    })
  },

  // 字段变化处理
  onFieldChange(e) {
    const { field } = e.currentTarget.dataset
    const  value  = e.detail

    this.setData({
      [`formData.${field}`]: value,
    })

    this.checkCanSubmit()
  },

  // 显示房屋选择器
  showHousePicker() {
    this.setData({ showHouse: true })
  },

  // 关闭房屋选择器
  closeHousePicker() {
    this.setData({ showHouse: false })
  },

  // 确认房屋选择
  onHouseConfirm(e) {
    const { value, index } = e.detail
    const selectedHouse = this.data.houseOptions[index]

    this.setData({
      "formData.houseId": selectedHouse.value,
      "formData.houseAddress": selectedHouse.text,
      showHouse: false,
    })

    this.checkCanSubmit()
  },

  // 检查是否可以提交
  checkCanSubmit() {
    const { formData } = this.data
    const isValid = formData.houseId && formData.description.trim().length >= 10

    this.setData({
      canSubmit: isValid,
    })
  },

  // 提交表单
  onSubmit() {
    if (!this.data.canSubmit) {
      wx.showToast({
        title: "请完善必填信息",
        icon: "none",
      });
      return;
    }
  
    this.setData({ submitting: true });
  
    const { formData, fileList } = this.data;
  
    // 调用 API 提交表单
    api.post("/property/complaint", formData).then((res) => {
      if (res.code === 200) {
        // 提交成功
        wx.showToast({
          title: "提交成功",
          icon: "success",
        });
  
        setTimeout(() => {
          wx.navigateBack(); // 返回上一页
        }, 1500);
      } else {
        // 提交失败
        wx.showToast({
          title: res.msg || "提交失败",
          icon: "none",
        });
      }
    }).catch((err) => {
      console.error(err);
      wx.showToast({
        title: "提交失败，请稍后再试",
        icon: "none",
      });
    }).finally(() => {
      this.setData({ submitting: false });
    });
  },
})
